##
## 4 f r
## 103 106 25
# aesthetic mapping gives multiple groups for each bar
p1 <- ggplot(d,aes(x=drv,fill=fl)) + geom_bar()
print(p1)# stacked, but need to adjust color transparency, which is "alpha"
p1 <- ggplot(d,aes(x=drv,fill=fl)) + geom_bar(alpha = 0.3, position="identity")
print(p1)# better to use position = fill for stacking, but with equivalent height
p1 <- ggplot(d,aes(x=drv,fill=fl)) + geom_bar(position="fill")
print(p1)# best to use position = dodge for multiple bars
p1 <- ggplot(d,aes(x=drv,fill=fl)) + geom_bar(position="dodge",color="black",size=1)
print(p1)# more typical "bar plot" has heights as the values themselves
dTiny <- tapply(X=d$hwy,INDEX=as.factor(d$fl),FUN=mean) #calculate the means
dTiny <- data.frame(hwy=dTiny) # create a single-column data frame
dTiny <- cbind(fl=row.names(dTiny),dTiny) #
p2 <- ggplot(dTiny, aes(x=fl,y=hwy,fill=fl)) +
geom_col()
print(p2)# basic boxplot is simple and informative
p1 <- ggplot(d,aes(x=fl,y=hwy,fill=fl)) +
geom_boxplot()
print(p1)# now overlay the raw data
p1 <- ggplot(d,aes(x=fl,y=hwy)) +
geom_boxplot(fill="thistle",outlier.shape=NA) +
# geom_point()
geom_point(position=position_jitter(width=0.1,height=0.7),color="grey60",size=2)
print(p1) ### Using Colors
# gray function versus gray colors
# built in greys (0 = black, 100 = white
my_greys <- c("grey20","grey50","grey80")
demoplot(my_greys,"bar")# converting color plots to black and white
p1 <- ggplot(d,aes(x=as.factor(cyl),y=cty,fill=as.factor(cyl))) + geom_boxplot()
plot(p1)# default colors look identical in black white
p1_des<- colorblindr::edit_colors(p1, desaturate)
plot(p1_des)# custom colors not pretty, but convert ok to bw
p2 <- p1 + scale_fill_manual(values=c("red","blue","green","yellow"))
plot(p2)x1 <- rnorm(n=100,mean=0)
x2 <- rnorm(n=100,mean=2.7)
dFrame <- data.frame(v1=c(x1,x2))
lab <- rep(c("Control","Treatment"),each=100)
dFrame <- cbind(dFrame,lab)
str(dFrame)## 'data.frame': 200 obs. of 2 variables:
## $ v1 : num -0.0306 1.4022 0.166 2.5987 -1.1781 ...
## $ lab: Factor w/ 2 levels "Control","Treatment": 1 1 1 1 1 1 1 1 1 1 ...
h1 <- ggplot(dFrame,aes(x=v1,fill=lab))
h1 + geom_histogram(position="identity",alpha=0.5,color="black") d <- mpg
# --------- discrete classification
# scale_fill_manual for boxplots,bars
# scale_color_manual for points, lines
# boxplot no color
p_fil <- ggplot(d,aes(x=as.factor(cyl),y=cty))
p_fil + geom_boxplot()# boxplot default ggplot fill
p_fil <- ggplot(d,aes(x=as.factor(cyl),y=cty,fill=as.factor(cyl))) + geom_boxplot()
plot(p_fil)# create custom color palette
my_cols <- c("red","brown","blue","orange")
# boxplot with custom colors for fill
p_fil + scale_fill_manual(values=my_cols)# scatterplot default ggplot colors
p_col <- ggplot(d,aes(x=displ,y=cty,col=as.factor(cyl))) + geom_point(size=3)
plot(p_col)# ------- continuous classification (color gradient)
# default color gradient
p_grad <- ggplot(d,aes(x=displ,y=cty,col=hwy)) + geom_point(size=3)
plot(p_grad)# custom diverging gradient (3-colors)
mid <- median(d$cty)
p_grad + scale_color_gradient2(midpoint=mid,
low="blue",
mid="white",
high="red")# custom diverging gradient (n-colors
p_grad + scale_color_gradientn(colors=c("blue","green","yellow","purple","orange"))## $BottleRocket1
## [1] "#A42820" "#5F5647" "#9B110E" "#3F5151"
## [5] "#4E2A1E" "#550307" "#0C1707"
##
## $BottleRocket2
## [1] "#FAD510" "#CB2314" "#273046" "#354823"
## [5] "#1E1E1E"
##
## $Rushmore1
## [1] "#E1BD6D" "#EABE94" "#0B775E" "#35274A"
## [5] "#F2300F"
##
## $Rushmore
## [1] "#E1BD6D" "#EABE94" "#0B775E" "#35274A"
## [5] "#F2300F"
##
## $Royal1
## [1] "#899DA4" "#C93312" "#FAEFD1" "#DC863B"
##
## $Royal2
## [1] "#9A8822" "#F5CDB4" "#F8AFA8" "#FDDDA0"
## [5] "#74A089"
##
## $Zissou1
## [1] "#3B9AB2" "#78B7C5" "#EBCC2A" "#E1AF00"
## [5] "#F21A00"
##
## $Darjeeling1
## [1] "#FF0000" "#00A08A" "#F2AD00" "#F98400"
## [5] "#5BBCD6"
##
## $Darjeeling2
## [1] "#ECCBAE" "#046C9A" "#D69C4E" "#ABDDDE"
## [5] "#000000"
##
## $Chevalier1
## [1] "#446455" "#FDD262" "#D3DDDC" "#C7B19C"
##
## $FantasticFox1
## [1] "#DD8D29" "#E2D200" "#46ACC8" "#E58601"
## [5] "#B40F20"
##
## $Moonrise1
## [1] "#F3DF6C" "#CEAB07" "#D5D5D3" "#24281A"
##
## $Moonrise2
## [1] "#798E87" "#C27D38" "#CCC591" "#29211F"
##
## $Moonrise3
## [1] "#85D4E3" "#F4B5BD" "#9C964A" "#CDC08C"
## [5] "#FAD77B"
##
## $Cavalcanti1
## [1] "#D8B70A" "#02401B" "#A2A475" "#81A88D"
## [5] "#972D15"
##
## $GrandBudapest1
## [1] "#F1BB7B" "#FD6467" "#5B1A18" "#D67236"
##
## $GrandBudapest2
## [1] "#E6A0C4" "#C6CDF7" "#D8A499" "#7294D4"
##
## $IsleofDogs1
## [1] "#9986A5" "#79402E" "#CCBA72" "#0F0D0E"
## [5] "#D9D0D3" "#8D8680"
##
## $IsleofDogs2
## [1] "#EAD3BF" "#AA9486" "#B6854D" "#39312F"
## [5] "#1C1718"
#### Making a heat map
xVar <- 1:30
yVar <- 1:5
myData <- expand.grid(xVar=xVar,yVar=yVar)
head(myData)## xVar yVar
## 1 1 1
## 2 2 1
## 3 3 1
## 4 4 1
## 5 5 1
## 6 6 1
## xVar yVar zVar
## 1 1 1 1.93
## 2 2 1 4.78
## 3 3 1 4.30
## 4 4 1 3.63
## 5 5 1 6.51
## 6 6 1 5.77
#p4 <- ggplot(myData,aes(x=xVar,y=yVar,fill=zVar)) +
#geom_tile()
p4 + scale_fill_gradient2(midpoint=19,low="brown",mid=grey(0.8),high="darkblue")